package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.DmRoleScriptResponse;
import com.zzhoujay.richtext.RichText;

import java.util.List;
import java.util.stream.Collectors;

public class RoleScriptDetailDialog extends Dialog {

    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle, tvContent, tvNoData;
    private ImageView ivClose, ivMenu;
    private LinearLayout llMenu;

    private List<DmRoleScriptResponse.DataXX.ScriptData.DataX.Data> dataList;
    private List<String> titleList;
    public static int ROLE_SCRIPT_MENU_CHECK = 0;

    private MenuPopup mMenuPopup;

    public RoleScriptDetailDialog(@NonNull Context context) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        initContentView();
        init(context);
        RichText.initCacheDir(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_dm_script, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        tvContent = dialogView.findViewById(R.id.tvContent);
        tvNoData = dialogView.findViewById(R.id.tvNoData);
        ivClose = dialogView.findViewById(R.id.ivClose);
        ivMenu = dialogView.findViewById(R.id.ivMenu);
        llMenu = dialogView.findViewById(R.id.llMenu);

        ivClose.setOnClickListener(v -> {
            RichText.clear(this);
            cancel();
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(List<DmRoleScriptResponse.DataXX.ScriptData.DataX.Data> data) {
        dataList = data;
        //该方法6.0不兼容
        tvContent.setText("");
        titleList = dataList.stream().map(DmRoleScriptResponse.DataXX.ScriptData.DataX.Data::getTitle).collect(Collectors.toList());
        tvTitle.setText(dataList.get(ROLE_SCRIPT_MENU_CHECK).getTitle());
        RichText.from(dataList.get(ROLE_SCRIPT_MENU_CHECK).getContent()).bind(mContext).into(tvContent);

        llMenu.setOnClickListener(v -> {
            mMenuPopup = new MenuPopup(mContext, titleList, ROLE_SCRIPT_MENU_CHECK, new MenuPopup.OnSelectListener() {
                @Override
                public void onSelect(int position) {
                    ROLE_SCRIPT_MENU_CHECK = position;
                    tvTitle.setText(dataList.get(position).getTitle());
                    tvContent.setText("");
                    if (!TextUtils.isEmpty(dataList.get(position).getContent())) {
                        tvContent.setVisibility(View.VISIBLE);
                        tvNoData.setVisibility(View.GONE);
                        RichText.from(dataList.get(position).getContent()).bind(mContext).into(tvContent);
                    } else {
                        tvContent.setVisibility(View.GONE);
                        tvNoData.setVisibility(View.VISIBLE);
                    }
                    mMenuPopup.dismiss();
                }
            });
            mMenuPopup.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            mMenuPopup.setHeight(mMenuPopup.getContentView().getMeasuredHeight());
            int[] location = new int[2];
            llMenu.getLocationInWindow(location);
            if (!mMenuPopup.isShowing()) {
                mMenuPopup.showAtLocation(llMenu, Gravity.NO_GRAVITY,
                        location[0], location[1] - mMenuPopup.getHeight());
            }
        });

    }
}
